/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.cadmiumjdbc;

import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLXML;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.Channel;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Custom;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;
import fr.x9c.cadmium.primitives.cadmium.Cadmium;

/**
 * This class provides the primitives related to the <tt>java.sql.SQLXML</tt> class.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.1
 * @since 1.1
 */
@PrimitiveProvider
public final class SQLXMLs {

    /**
     * No instance of this class.
     */
    private SQLXMLs() {
    } // end empty constructor

    /**
     * Frees the passed SQL XML.
     * @param ctxt context
     * @param sqlxml sqlxml
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_sqlxml_free(final CodeRunner ctxt,
                                                final Value sqlxml)
        throws Fail.Exception {
        try {
            final SQLXML s = (SQLXML) sqlxml.asBlock().asCustom();
            s.free();
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_sqlxml_free(CodeRunner, Value)'

    /**
     * Returns a binary stream allowing to read the value of the passed SQL XML.
     * @param ctxt context
     * @param sqlxml sqlxml
     * @return a binary stream allowing to read the value of the passed SQL XML
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_sqlxml_get_binary_stream(final CodeRunner ctxt,
                                                             final Value sqlxml)
        throws Fail.Exception {
        try {
            final SQLXML s = (SQLXML) sqlxml.asBlock().asCustom();
            final InputStream is = s.getBinaryStream();
            final Channel ch = new Channel(is);
            ctxt.getContext().addChannel(ch);
            final Block res = Block.createCustom(Custom.CHANNEL_SIZE,
                                                 Custom.CHANNEL_OPS);
            res.setCustom(ch);
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_sqlxml_get_binary_stream(CodeRunner, Value)'

    /**
     * Returns the context of the passed SQL XML as a string.
     * @param ctxt context
     * @param sqlxml sqlxml
     * @return the context of the passed SQL XML as a string
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_sqlxml_get_string(final CodeRunner ctxt,
                                                      final Value sqlxml)
        throws Fail.Exception {
        try {
            final SQLXML s = (SQLXML) sqlxml.asBlock().asCustom();
            final Block res = Block.createString(s.getString());
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_sqlxml_get_string(CodeRunner, Value)'

    /**
     * Returns a binary stream allowing to write the value of the passed SQL XML.
     * @param ctxt context
     * @param sqlxml sqlxml
     * @return a binary stream allowing to write the value of the passed SQL XML
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_sqlxml_set_binary_stream(final CodeRunner ctxt,
                                                             final Value sqlxml)
        throws Fail.Exception {
        try {
            final SQLXML s = (SQLXML) sqlxml.asBlock().asCustom();
            final OutputStream os = s.setBinaryStream();
            final Channel ch = new Channel(os);
            ctxt.getContext().addChannel(ch);
            final Block res = Block.createCustom(Custom.CHANNEL_SIZE,
                                                 Custom.CHANNEL_OPS);
            res.setCustom(ch);
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_sqlxml_set_binary_stream(CodeRunner, Value)'

    /**
     * Sets the value of the passed SQL XML.
     * @param ctxt context
     * @param sqlxml sqlxml
     * @param bytes new value
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_sqlxml_set_string(final CodeRunner ctxt,
                                                      final Value sqlxml,
                                                      final Value bytes)
        throws Fail.Exception {
        try {
            final SQLXML s = (SQLXML) sqlxml.asBlock().asCustom();
            s.setString(bytes.asBlock().asString());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_sqlxml_set_string(CodeRunner, Value, Value)'

} // end class 'SQLXMLs'
